<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-07-25 15:38:21
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-07-25 16:44:49
 * @Description:
 *
-->

<template>
  <el-form
    ref="form"
    class="block-form-huawei"
    :rules="rules"
    :model="form"
    label-width="auto"
    size="medium"
  >
    <el-form-item
      v-if="scene != 'orchestrate'"
      class="item"
      label="云服务商"
      prop="cloudProvider"
    >
      <el-select
        v-model="form.cloudProvider"
        placeholder="请选择云服务商"
        style="width: 100%"
        :disabled="true"
      >
        <el-option
          v-for="item in cloudProviderOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      v-if="scene != 'orchestrate'"
      class="item right"
      label="云账户"
      prop="cloudAccountName"
    >
      <el-select
        v-model="form.cloudAccountName"
        placeholder="请选择云账户"
        style="width: 100%"
        no-data-text="请选择云服务商"
      >
        <el-option
          v-for="item in cloudAccountNameOptions"
          :key="item.cloudAccountName"
          :label="item.cloudAccountName"
          :value="item.cloudAccountName"
        />
      </el-select>
    </el-form-item>
    <el-form-item class="item" label="区域" prop="cpRegionId">
      <el-select
        v-model="form.cpRegionId"
        placeholder="请选择区域"
        no-data-text="请选择合适的云服务商"
        style="width: 100%"
        @change="regionChange"
      >
        <el-option
          v-for="item in regionOptions"
          :key="item.cpId"
          :label="item.desc"
          :value="item.cpId"
        />
      </el-select>
    </el-form-item>
    <el-form-item class="item right" label="可用区" prop="zone">
      <el-select
        v-model="form.zone"
        placeholder="请选择可用区"
        no-data-text="请先选择区域"
        style="width: 100%"
        @change="zoneChange"
      >
        <el-option
          v-for="item in zoneOptions"
          :key="item.key"
          :label="item.text"
          :value="item.key"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="云盘" prop="type">
      <el-row type="flex" justify="center">
        <el-select
          v-model="form.type"
          placeholder="请选择云盘"
          style="flex: 1; margin-right: 10px"
        >
          <el-option
            v-for="item in sysDiskTypeOptions"
            :key="item.zhName"
            :label="item.zhName"
            :value="item.zhName"
          />
        </el-select>
        <el-input-number
          v-model="form.size"
          style="flex: 1"
          :min="20"
          :max="500"
          :precision="0"
          label="云盘大小"
        />
        <span style="margin-left: 10px">GB</span>
      </el-row>
    </el-form-item>
    <el-form-item label="是否加密" prop="encrypted">
      <el-radio-group v-model="form.encrypted" size="small">
        <el-radio
          v-for="item in encryptedOptions"
          :key="item.value"
          :label="item.value"
          border
        >{{ item.label }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="描述" prop="desc">
      <el-input
        v-model="form.desc"
        placeholder="请输入描述"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
  </el-form>
</template>

<script>
import resSelectorMixin from '@/views/mixin/resSelector.js'
import { getRegionList, getBlockVolumeType } from '@/api/cn-resource'
import debounce from 'lodash/debounce'

export default {
  name: 'BlockFormAliyun',
  components: {},
  mixins: [resSelectorMixin],
  props: {
    scene: {
      type: String,
      default: 'create'
    },
    info: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      skipGetCloudAccount: false,
      cloudProviderType: 'public',
      regionOptions: [],
      zoneOptions: [],
      encryptedOptions: [
        {
          label: '否',
          value: false
        },
        {
          label: '是',
          value: true
        }
      ],
      sysDiskTypeOptions: [],
      form: {
        cloudProvider: '',
        cloudAccountName: '',
        cpRegionId: '', // 区域
        zone: '', // 可用区
        encrypted: false, // 是否加密
        type: '', // 云盘类型
        size: 20, // 云盘大小
        desc: ''
      },
      // 校验规则
      rules: {
        cloudProvider: [
          { required: true, message: '请选择云平台', trigger: 'change' }
        ],
        cloudAccountName: [
          { required: true, message: '请选择云账户', trigger: 'change' }
        ],
        cpRegionId: [
          { required: true, message: '请选择区域', trigger: 'change' }
        ],
        zone: [{ required: true, message: '请选择可用区', trigger: 'change' }],
        encrypted: [
          { required: true, message: '请选择是否加密', trigger: 'change' }
        ],
        type: [{ required: true, message: '请选择云盘', trigger: 'change' }]
      },
      sysDiskTypeQueryDebounce: null
    }
  },
  computed: {
    /**
     * @description: 云盘相关参数
     * @return {*}
     */
    sysDiskTypeParam() {
      return {
        cloudAccountName: this.form.cloudAccountName,
        cpRegionId: this.form.cpRegionId,
        zone: this.form.zone
      }
    }
  },
  watch: {
    /**
     * @description: 监听
     * @return {*}
     */
    info: {
      handler(newVal) {
        if (newVal) {
          this.form = Object.assign(this.form, this.info)
        }
      },
      immediate: true,
      deep: true
    },
    /**
     * @description: 监听云盘相关参数
     * @return {*}
     */
    sysDiskTypeParam: {
      handler(newVal) {
        if (
          this.form.cloudAccountName &&
          this.form.cpRegionId &&
          this.form.zone
        ) {
          if (!this.sysDiskTypeQueryDebounce) {
            // 创建防抖函数
            this.sysDiskTypeQueryDebounce = debounce(() => {
              this.getDiskType()
            }, 300)
          }
          this.sysDiskTypeQueryDebounce()
        }
      },
      immediate: true,
      deep: true
    },
    /**
     * @description: 云商监听
     * @return {*}
     */
    'form.cloudProvider': {
      handler(newVal) {
        if (newVal) {
          this.getRegionList()
        }
      },
      immediate: true
    }
  },
  created() {
    this.skipGetCloudAccount = this.scene === 'orchestrate'
  },
  methods: {
    /**
     * @description: 获取云盘类型
     * @return {*}
     */
    getDiskType() {
      getBlockVolumeType({
        cloudProvider: this.form.cloudProvider,
        cloudAccountName: this.form.cloudAccountName,
        cpRegionId: this.form.cpRegionId,
        cpZoneId: this.form.zone
      }).then((res) => {
        this.sysDiskTypeOptions = Array.isArray(res.data) ? res.data : []
      })
    },
    /**
     * @description: 可用区变化
     * @param {*} val
     * @return {*}
     */
    zoneChange() {
      // 清空云盘
      this.form.type = ''
      this.sysDiskTypeOptions = []
    },
    /**
     * @description: 区域变化
     * @param {*} val
     * @return {*}
     */
    regionChange(val) {
      // 刷新可用区
      this.form.zone = ''
      this.findZoneOptions()
      // 清空云盘
      this.form.type = ''
      this.sysDiskTypeOptions = []
    },
    /**
     * @description: 查找可用区
     * @return {*}
     */
    findZoneOptions() {
      // 查找已选择
      const regionSelect = this.regionOptions.find((item) => {
        return this.form.cpRegionId === item.cpId
      })
      // 设置可用区选项
      if (regionSelect && Array.isArray(regionSelect.zones)) {
        this.zoneOptions = regionSelect.zones.filter((item) => {
          return item.val
        })
      } else {
        this.zoneOptions = []
      }
    },
    /**
     * @description: 获取区域
     * @return {*}
     */
    getRegionList() {
      getRegionList({
        keyword: this.form.cloudProvider,
        pageNum: 1,
        pageSize: 100
      }).then((res) => {
        if (res.data) {
          this.regionOptions = Array.isArray(res.data.data)
            ? res.data.data
            : []
          if (this.form.cpRegionId) {
            this.findZoneOptions()
          }
        } else {
          this.regionOptions = []
        }
      })
    },
    /**
     * @description: 校验
     * @return {*}
     */
    validateForm(callback) {
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          callback(this.form)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.block-form-huawei {
  .item {
    display: inline-block;
    width: calc(50% - 7.5px);
    &.right {
      margin-left: 15px;
    }
  }
}
</style>
